package cocurrency.atomic;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by luzl on 4/7/2017.
 */
public class AtomicThread extends Thread {
    //public volatile static int count;
    public static AtomicInteger count;
    private CyclicBarrier cyclicBarrier;
    private String name;
    public AtomicThread(CyclicBarrier cyclicBarrier, AtomicInteger count, String name){
        this.cyclicBarrier=cyclicBarrier;
        AtomicThread.count=count;
        this.name=name;
    }

    public void run(){
        try {
            System.out.println(this.name+" waiting!");
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(this.name+" running!");
        try {
            currentThread().sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count.getAndIncrement();
        //System.out.println("Calculate is done, count="+this.count);
    }
}
